#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct testee
{
    string num;
    int score;
    int fr, lr, ln;
};
bool cmp(testee &t1, testee &t2)
{
    return t1.score == t2.score ? t1.num < t2.num : t1.score > t2.score;
}
int main()
{
    // ifstream in("case.txt");
    int n, k;
    cin >> n;
    vector<testee> rank;
    for (int i = 0; i < n; i++)
    {
        cin >> k;
        testee *t;
        vector<testee> localrank;
        for (int j = 0; j < k; j++)
        {
            t = new testee();
            cin >> t->num >> t->score;
            t->ln = i + 1;
            localrank.push_back(*t);
        }
        sort(localrank.begin(), localrank.end(), cmp);
        int p = 1, q = 1;
        for (auto it = localrank.begin(); it < localrank.end(); it++)
        {
            if (it != localrank.begin() && it->score != (it - 1)->score)
                q = p;
            it->lr = q;
            p++;
        }
        rank.insert(rank.end(), localrank.begin(), localrank.end());
    }
    sort(rank.begin(), rank.end(), cmp);
    cout << rank.size() << endl;
    int p = 1, q = 1;
    for (auto it = rank.begin(); it < rank.end(); it++)
    {
        if (it != rank.begin() && it->score != (it - 1)->score)
            q = p;
        it->fr = q;
        p++;
        cout << it->num << " " << it->fr << " " << it->ln << " " << it->lr << endl;
    }
    return 0;
}